package mesweb.mesweb.controller.produce;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.dto.ProducePlan.DayReportDetailConditionDto;
import mesweb.mesweb.entity.produce.ProduceDayReport;
import mesweb.mesweb.service.produce.IProduceDayReportService;
import mesweb.mesweb.vo.ProducePlan.DayReportDetailResultVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 日报工
 * @Author: jeecg-boot
 * @Date:   2021-08-17
 * @Version: V1.0
 */

@Api(tags="日报工")
@RestController
@RequestMapping("/mes/produceDayReport")
public class ProduceDayReportController {
	@Autowired
	private IProduceDayReportService produceDayReportService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<ProduceDayReport> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = produceDayReportService.list();
        } else {
            QueryWrapper<ProduceDayReport> queryWrapper = new QueryWrapper<>();
            data = produceDayReportService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param produceDayReport
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="日报工-分页列表查询", notes="日报工-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(ProduceDayReport produceDayReport,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<ProduceDayReport> queryWrapper = new QueryWrapper<>();
		Page<ProduceDayReport> pageProduceDayReport = new Page<ProduceDayReport>(page, size);
		IPage<ProduceDayReport> pageList = produceDayReportService.getProduceDayReport(pageProduceDayReport, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param produceDayReport
	 * @return
	 */
	@ApiOperation(value="日报工-编辑", notes="日报工-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody ProduceDayReport produceDayReport) {
		boolean result = false;
        if(produceDayReport.getId() == 0L) {
			QueryWrapper<ProduceDayReport> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("order_no", produceDayReport.getOrderNo());
			queryWrapper.eq("product_no", produceDayReport.getProductNo());
			queryWrapper.eq("procedure_no", produceDayReport.getProcedureNo());
			queryWrapper.eq("last_report_date", produceDayReport.getLastReportDate());
			ProduceDayReport mesTechnologyFlow1 = produceDayReportService.getOne(queryWrapper);
			if (mesTechnologyFlow1 != null) {
				String msg = StrUtil.format("订单{},产品{},工序{},最晚上报日期或者名称{}已经存在！", mesTechnologyFlow1.getOrderNo(),mesTechnologyFlow1.getProductNo(),mesTechnologyFlow1.getProcedureNo(),mesTechnologyFlow1.getLastReportDate());
				throw new ApiException(msg);
			}
             result = produceDayReportService.save(produceDayReport);
        }
        else
        {
            result = produceDayReportService.updateById(produceDayReport);
        }
        return CommonResult.success(result);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="日报工-通过id删除", notes="日报工-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		produceDayReportService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="日报工-批量删除", notes="日报工-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("ProduceDayReportIDs") @RequestBody List<Long> ids) {
		this.produceDayReportService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="日报工-通过id查询", notes="日报工-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		ProduceDayReport produceDayReport = produceDayReportService.getById(id);
		return CommonResult.success(produceDayReport);
	}

	/**
	 * 获取某月某订单某产品的进度报表
	 * @param  dayReportDetailConditionDto
	 * @return
	 */
	@ApiOperation(value = "订单产品排产-获取某月某订单某产品的进度报表", notes = "订单产品排产-获取某月某订单某产品的进度报表")
	@PostMapping(value = "/statProduceProcess")
	public CommonResult<?> statProduceProcess(@RequestBody DayReportDetailConditionDto dayReportDetailConditionDto) {
		List<DayReportDetailResultVo> result =  produceDayReportService.statProduceProcess(dayReportDetailConditionDto);
		return CommonResult.success(result);
	}

}
